

//** I N C L U D E S **********************************************************/
#include <p24fj16ga002.h>
#include "Config.h"
#include "1wire.h"
#include "uart.h"

//******* G E N E R I C   D E F I N I T I O N S ************************************************/
//#define BAUD_CONTROL			00
#define READ_COMMAND_DS2411  	0x33         // Command to read the 64-bit serial number from 1-wire slave device.
#define SKIP_ROM 				0xCC
#define CONVERT_T 				0x44
#define READ_ROM 				0x33
#define READ_SCRATCHPAD 		0xBE


//****** V A R I A B L E S ********************************************************/
unsigned char welcome_message[] = "I AM A MASTER DEVICE\r\n";
unsigned char not_present[] = "SLAVE Device is not present\r\n";
unsigned char dev_present [] = "SLAVE Device is present\r\n";
unsigned char ser_num [] = "SERIAL NUMBER:  \r\n";
int i;

//*************************************************************************

//****** P R O T O T Y P E S ******************************************************/
void PutString (unsigned char *mess);
unsigned char Getchar(void);
void Putchar (unsigned char);
unsigned char Detect_Slave_Device(void);
void hex_ascii(unsigned char);


_CONFIG2( IESO_OFF & FNOSC_PRIPLL & FCKSM_CSDCMD & OSCIOFNC_ON & POSCMOD_HS & I2C1SEL_PRI)
_CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & ICS_PGx1 & FWDTEN_OFF & WINDIS_OFF);


/**********************************************************************
* Function:        main function.
* PreCondition:    None
* Input:		   None
* Output:		   None	
* Overview:		   1-wire protocol develop for DS2411
***********************************************************************/

void main(void)
{
	init();

		
	   unsigned char temp;
	   

	   float temp_celsius;
	   unsigned char serial_number[8];
	   unsigned char str[16];
	   unsigned char t_conv[9];


   uart1_tx_str((const char *) welcome_message);				// Display Welocme Message on Terminal.

   if(!Detect_Slave_Device())	    		// Is slave present???
   		uart1_tx_str((const char *) not_present);				// Send message to terminal showing 1-wire slave device not present.
   else
   		uart1_tx_str((const char *) dev_present);				// Send message to terminal showing 1-wire slave device present.
   		
   OW_write_byte (READ_COMMAND_DS2411);		// Send a command to read a serial number
   
   
   uart1_tx_str((const char *) ser_num);						// Display the message on terminal.
	
   for(temp = 0; temp<8; temp++)
   		serial_number[temp] = OW_read_byte();	// Read 64-bit registration (48-bit serial number) number from 1-wire Slave Device

   for(temp = 0; temp<8; temp++)
	   {
		//hex_ascii(serial_number[temp]);			//Convert Hex Value into ASCII and send to terminal
		byte2str(serial_number[temp],&str[2*temp]);
	}
	uart1_tx_str((const char *) "Seriale: ");
	uart1_tx_str(str);	
   	uart1_tx_str((const char *) "\r\n");
   	
   Detect_Slave_Device();
   
   OW_write_byte (SKIP_ROM);		// Send a command to read a serial number
   OW_write_byte  (CONVERT_T);
   
   for(i=0;i<1000;i++)
	   Delay_Us(1200);
	
   temp_celsius = Read_Temperature_DS18S20();
   ftoa(temp_celsius,&t_conv,2,'F');
   
   uart1_tx_str((const char *) "Temperatura: ");
   
   uart1_tx_str((const char * ) t_conv);
   
   

   
   
   while(1);
  
} // END OF MAIN

/**********************************************************************
* Function:        unsigned char Detect_Slave_Device(void)
* PreCondition:    None
* Input:		   None	
* Output:		   1 - Not Present   0 - Present 	
* Overview:   	   To check the presence of slave device.    
***********************************************************************/

unsigned char Detect_Slave_Device(void)
{
	if (!OW_reset_pulse())
		return HIGH;
	else 		
		return LOW;
}	



/**********************************************************************
* Function:        void wait (unsigned char delay_time)
* PreCondition:    None
* Input:		   Required Delay value	
* Output:
* Side Effects:
* Overview:
*					Function delay_5Us with clock 25MHz;
*					calculation: 
*					20*0x16 + (7+n)*0.16*delay_time in Us
*					20	: single instructions including call and return.
*					n   : 1 (number of NOPs).
*					8	: (7 + n) ....7 Instructions used by 'for' loop and 1 nop instruction.
* 					0.16: (1/25000000)*4 ...Clock 25MHz
* 					delay_needed in Us.
* 					Calculated delay_time_value in Us.
*					delay_time_value = (delay_needed - (20*0.16))/(8*0.16)
*					delay_time_value = (delay_needed - 3.2) / (1.28)
************************************************************************/
//#ifdef FOR_LOOP_DELAY
//void wait (unsigned char delay_time)
//{
//	unsigned char temp;
//	
//	for(temp = 0; (temp<(delay_time)); temp++)
//	_asm nop _endasm
//}	
//#endif

/**********************************************************************
* Function:        void hex_ascii(unsigned char display_data)
* PreCondition:    None
* Input:		   HEX Value 
* Output:		   None	 
* Side Effects:
* Overview:		 Convert Hex Value to ASCII and send to PC;
***********************************************************************/
/*
void hex_ascii(unsigned char display_data)
{
	unsigned char temp;
	temp = ((display_data & 0xF0)>>4);
	if (temp <= 0x09)
		uart1_tx_byte(temp+'0');	
	else
		uart1_tx_byte(temp+'0'+0x07);	

	temp = display_data & 0x0F;
	if (temp <= 0x09)
		uart1_tx_byte(temp+'0');	
	else
		uart1_tx_byte(temp+'0'+0x07);	
	
	uart1_tx_byte('\r');
	uart1_tx_byte('\n');
}	
	
*/